package com.training.bt.service;

import com.training.bt.entity.FriendRelation;
import com.training.bt.entity.User;
import com.training.bt.mapper.FriendRelationMapper;
import com.training.bt.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class FriendService implements IFriendService {

    @Autowired
    private FriendRelationMapper friendRelationMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void addFriend(Long userId, Long friendId) {
        // 验证用户存在
        User user = userMapper.selectById(userId);
        User friend = userMapper.selectById(friendId);
        if (user == null || friend == null) {
            throw new IllegalArgumentException("用户不存在");
        }

        // 避免自己添加自己为好友
        if (Objects.equals(userId, friendId)) {
            throw new IllegalArgumentException("不能添加自己为好友");
        }

        // 检查是否已为好友
        if (isFriend(userId, friendId)) {
            return;
        }

        // 添加双向好友关系
        FriendRelation relation1 = new FriendRelation();
        relation1.setUserId(userId);
        relation1.setFriendId(friendId);
        relation1.onCreate();
        friendRelationMapper.insert(relation1);

        FriendRelation relation2 = new FriendRelation();
        relation2.setUserId(friendId);
        relation2.setFriendId(userId);
        relation2.onCreate();
        friendRelationMapper.insert(relation2);
    }

    @Override
    @Transactional
    public void removeFriend(Long userId, Long friendId) {
        // 验证用户存在
        User user = userMapper.selectById(userId);
        User friend = userMapper.selectById(friendId);
        if (user == null || friend == null) {
            throw new IllegalArgumentException("用户不存在");
        }

        // 删除双向好友关系
        FriendRelation relation1 = new FriendRelation();
        relation1.setUserId(userId);
        relation1.setFriendId(friendId);
        
        FriendRelation relation2 = new FriendRelation();
        relation2.setUserId(friendId);
        relation2.setFriendId(userId);

        friendRelationMapper.deleteById(relation1);
        friendRelationMapper.deleteById(relation2);
    }

    @Override
    public List<User> getFriends(Long userId) {
        List<Long> friendIds = friendRelationMapper.findFriendIdsByUserId(userId);
        List<User> friends = new ArrayList<>();

        for (Long friendId : friendIds) {
            User friend = userMapper.selectById(friendId);
            if (friend != null) {
                friends.add(friend);
            }
        }

        return friends;
    }

    @Override
    public boolean isFriend(Long userId, Long friendId) {
        return friendRelationMapper.existsByUserIdAndFriendId(userId, friendId);
    }
}